1025B - Weakened Common Divisor - CodeForces Solution


brute force greedy number theory *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define dbg(x) cout << "[" << #x << " = " << x << "] ";
#define ff first
#define ss second

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using vi = vector<int>;

using tii = tuple<int,int,int>;
// auto [a,b,c] = ...
// .insert({a,b,c})

const int oo = (int)1e9 + 5; //INF to INT
const ll OO = 0x3f3f3f3f3f3f3f3fLL; //INF to LL

/*
Muito tempo preso no problema? Próximo
Reler enunciado
casos de borda: n = 0, n = 1
cuidado com overflow
*/

set<int> p;

void fat(int x) {
    for(int u = 2; 1LL * u * u <= 1LL * x; u++) {
        if(x % u == 0) {
            p.insert(u);
            while(x % u == 0)
                x /= u;
        }
    }
    
    if(x > 1) {
        p.insert(x);
    }
    
}

void solve() {  

    int n;
    cin >> n;
        
    vector<pii> arr(n);
    
    for(auto &x : arr) {
        cin >> x.ff >> x.ss;
    }
    
    fat(arr[0].ff);
    fat(arr[0].ss);
    
    for(auto &prime : p) {
        
        bool ans = true;
        
        for(auto &k : arr) {
            if(k.ff % prime && k.ss % prime) {
                ans = false;
                break;
            }
        }
        
        if(ans) {
            cout << prime << "\n";
            return;
        }
    
    }    
    
    cout << -1 << "\n";


}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    while(t--)
        solve();


}


Comments

Submit
0 Comments
More Questions

566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins